<template>
	<view class="money">
		<view class="boxup">
			<image src="@/static/image/pjt.png">
			</image>
			<view class="zhanghu">
				<view class="money">{{ deposit }}</view>
				<view class="balance">保证金(元)</view>
			</view>
		</view>
		<view class="bj">
			<view class="table">
				<view class="tr">
					<view class="th">等级</view>
					<view class="th">描述</view>
					<view class="th">状态</view>
				</view>
				<view class="tr" v-for="(item, index) in table">
					<view class="td">
						<image :src="item.icon"></image>
					</view>
					<view class="td desc">
						{{ item.desc }}
					</view>
					<view class="td status">
						<!-- 立即缴纳 -->
						<!-- 退保证金 -->
						<up-button @click="handleClick(item)" :type="item.checked == 0?'primary':'info'"
							:text="item.checked == 0?'立即缴纳':'退保证金' "></up-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import user from '@/api/user.js'
	import utils from '@/utils/util.js'
	const deposit = ref(0)
	onMounted(() => {
		selfInfo()
	})
	uni.getStorageSync('userInfo')?.deposit
	const table = ref([])
	onMounted(() => {
		depositLevel()
	})
	const depositLevel = async () => {
		const {
			status,
			info,
			data
		} = await user.depositLevel()
		if (status == 1) {
			table.value = data
		}
	}
	const handleClick = async (val) => {
		const {
			status,
			info,
			data
		} = await user[val.checked == 0 ? 'rechargeDeposit' : 'withdrawalDeposit']({
			level_id: val.id
		})
		if (status == 1) {
			console.log('data', data);
			if (val.checked == 0) {
				uni.requestPayment({
					timeStamp: data.payment_data.timeStamp, //时间戳
					nonceStr: data.payment_data.nonceStr, //随机字符串
					package: data.payment_data.package, //prepay_id
					signType: data.payment_data.signType, //签名算法MD5
					paySign: data.payment_data.paySign, //签名
					success(res) {
						if (res.errMsg == "requestPayment:ok") {
							console.log('支付成功', res)
							depositLevel()
							selfInfo()
						} else {
							console.log('支付失败')
						}
					},
					fail(res) {
						console.log('支付失败', res)
					}
				})
			} else {
				depositLevel()
				selfInfo()
			}
		} else {
			utils.toast(info)
		}
	}

	const selfInfo = async () => {
		const {
			status,
			info,
			data
		} = await user.selfInfo()
		if (status == 1) {
			deposit.value = data.deposit
		}
	}
</script>

<style lang="scss" scoped>
	.money {
		padding: 24rpx;

		.boxup {
			position: relative;
			height: 308rpx;
			margin-bottom: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				position: absolute;
				left: -24rpx;
				width: calc(100% + 48rpx);
				height: 100%;
			}

			.zhanghu {
				position: inherit;
				text-align: center;

				.money {
					width: 100%;
					font-family: Alibaba PuHuiTi 3.0, Alibaba PuHuiTi 30;
					font-weight: 600;
					font-size: 54rpx;
					color: #FFFFFF;
					line-height: 76rpx;
				}

				.balance {
					width: 100%;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					line-height: 39rpx;
				}
			}
		}

		.bj {
			background: #FFFFFF;
			box-shadow: 0rpx 8rpx 16rpx 0rpx rgba(128, 128, 128, 0.06);
			border-radius: 32rpx;
			padding: 20rpx;

			.table {
				border: 2rpx solid #FFEBE2;
				border-radius: 16rpx;

				.tr {
					display: flex;
					border-bottom: 1px solid #FFEBE2;

					.th,
					.td {
						display: flex;
						align-items: center;
						justify-content: center;
						padding: 16rpx 26rpx;
						box-sizing: border-box;
					}

					.th:first-child,
					.td:first-child {
						width: 164rpx;
						border-right: 1px solid #FFEBE2;
					}

					.th:nth-child(2),
					.td:nth-child(2) {
						width: calc(100% - 164rpx - 200rpx);
					}

					.th:last-child,
					.td:last-child {
						width: 200rpx;
						border-left: 1px solid #FFEBE2;
					}

					.th {
						background: #FFEBE2;
						height: 74rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #F1692B;
						line-height: 39rpx;
					}

					.td {
						image {
							width: 106rpx;
							height: 48rpx;
						}
					}

					.desc {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #2C1F21;
						line-height: 39rpx;
					}
				}

				.tr:last-child {
					border: 0;
				}
			}
		}
	}
</style>